﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BBD_DAL.Entities;
using BBD_DAL.DBConnector;
using System.Data;
using MySql.Data.MySqlClient;

namespace BBD_DAL.DAO
{
	public class QuestionDao: IBaseDao<Question>
	{

		private DBConnection dbConnector;
		private Dictionary<string, string> queries;

		public QuestionDao(string connectionString)
		{
			this.Initialize(connectionString);
		}

		public void Initialize(string connectionString)
		{
			this.dbConnector = new DBConnection(connectionString);
			queries = new Dictionary<string, string>();
			queries.Add("Get", "SELECT * FROM pytania WHERE pytanie_id = @id;");
			queries.Add("Save", "UPDATE pytania SET pytanie = @question WHERE pytanie_id_id = @id;");
			queries.Add("Insert", "INSERT INTO pytania (pytanie) VALUES (@question);");
			queries.Add("Delete", "DELETE FROM pytania WHERE id = @id;");
			queries.Add("DeleteAll", "DELETE FROM pytania; ALTER TABLE pytania AUTO_INCREMENT = 1;");
			queries.Add("List", "SELECT * FROM pytania;");
		}

		public Question Get(long id)
		{
			DataTable data;
			this.dbConnector.RunQuery(this.queries["Get"], new MySqlParameter[]{
				new MySqlParameter(){ParameterName = "@id", Value = id, MySqlDbType = MySqlDbType.Int64}
			}, out data);

			List<Question> questions = this.Convert(data);

			return questions != null ? questions[0] : null;
		}

		public void Save(Question entity)
		{
			this.dbConnector.RunQuery(this.queries["Save"], new MySqlParameter[]{
				new MySqlParameter(){ParameterName = "@id", Value = entity.Id, MySqlDbType = MySqlDbType.Int64},
				new MySqlParameter(){ParameterName = "@question", Value = entity.QuestionText, MySqlDbType = MySqlDbType.VarChar}
			});
		}

		public Question Insert(Question entity)
		{
			long id;
			this.dbConnector.RunQuery(this.queries["Insert"], new MySqlParameter[]{				
				new MySqlParameter(){ParameterName = "@question", Value = entity.QuestionText, MySqlDbType = MySqlDbType.VarChar}
			}, out id);

			entity.Id = id;

			return entity;
		}

		public void Delete(long id)
		{
			this.dbConnector.RunQuery(this.queries["Delete"], new MySqlParameter[]{
				new MySqlParameter(){ParameterName = "@id", Value = id, MySqlDbType = MySqlDbType.Int64}
			});
		}

		public void Delete(Question entity)
		{
			this.Delete(entity.Id);
		}

		public void Delete()
		{
			this.dbConnector.RunQuery(this.queries["DeleteAll"], null);
		}

		public List<Question> List()
		{
			DataTable data;
			this.dbConnector.RunQuery(this.queries["List"], null, out data);
			return this.Convert(data);
		}

		private List<Question> Convert(DataTable data)
		{
			if (data == null || data.Rows == null || data.Rows.Count == 0)
				return null;

			List<Question> questions = new List<Question>();
			foreach (DataRow row in data.Rows)
			{
				Question question = new Question();
				question.Id = Int64.Parse(row["pytanie_id"].ToString());
				question.QuestionText = row["pytanie"].ToString();
				questions.Add(question);
			}
			return questions;
		}
	}
}
